package org.zfes.snowier.cloud.zbss.tts.router;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.zfes.snowier.cloud.zbss.tts.config.AppCtxUtil;
import org.zfes.snowier.cloud.zbss.tts.config.TssConfig;
import org.zfes.snowier.cloud.zbss.tts.util.ZDateUtil;
import org.zfes.snowier.cloud.zbss.tts.util.ZStrUtil;
import org.zfes.snowier.cloud.zbss.tts.util.datex.ZDateStyle;
import org.zfes.snowier.cloud.zbss.tts.vo.JsonMsg;
import org.zfes.snowier.cloud.zbss.tts.xf.WebIAT;
import org.zfes.snowier.cloud.zbss.tts.xf.WebTTS;


@Controller
@RequestMapping("/api")
public class ApiController {

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/textToAudio", method = {RequestMethod.POST,RequestMethod.GET},produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public @ResponseBody Object textToAudio(String text) {
		String saveFilePath=AppCtxUtil.cfg().getBean(TssConfig.class).getAudioSavePath();
		JsonMsg jsonMsg=WebTTS.doTans( saveFilePath , text);
		return jsonMsg;
	}
	 
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/audioToText", method = {RequestMethod.POST,RequestMethod.GET},produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public @ResponseBody Object audioToText(@RequestParam("file") MultipartFile file ) {
		
		String fileOriginName = file.getOriginalFilename();
		if(ZStrUtil.hasNoText(fileOriginName)) {
			return new JsonMsg("文件名称错误",300);
		}
		 String fileType = getFileType( fileOriginName);
		 String newFileName=ZDateUtil.getNowStr(ZDateStyle.YYYYMMDDHHMMSS)+"_"+getRandomString(6)+fileType;
		 
		String filePath=AppCtxUtil.cfg().getBean(TssConfig.class).getAudioUploadPath();
		File fileDir=new File(filePath);
		if(!fileDir.exists()) {
			fileDir.mkdir();
		}
		String absolutePath=filePath + File.separator + newFileName;
		 try {
			 File outFile = new File(absolutePath);
			 file.transferTo(outFile);
		 }catch (Exception e) {
			e.printStackTrace();
			return new JsonMsg("文件传输错误："+e.getMessage(),300);
		}
		
		 JsonMsg  jsonMsg =WebIAT.doTans(absolutePath);
		 
		return jsonMsg;
	}
	private  String getFileType(String fileName) {
        if(fileName!=null && fileName.indexOf(".")>=0) {
            return fileName.substring(fileName.lastIndexOf("."), fileName.length());
        }
        return "";
	}
	
	
	private static String getRandomString(int length){
	     String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	     Random random=new Random();
	     StringBuffer sb=new StringBuffer();
	     for(int i=0;i<length;i++){
	       int number=random.nextInt(62);
	       sb.append(str.charAt(number));
	     }
	     return sb.toString();
	 }
}
